-
Notifications
You must be signed in to change notification settings - Fork 474
Added support for Maven POM sorting/formatting #946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
22ddb38
Added support for Maven POM sorting/formatting
tisoft 503f4c0
load SortPom from JarState
tisoft 8b7c3eb
use FormatterFunc and SortPomState from current ClassLoader
tisoft c0d1b68
Fixed SpotBugs errors
tisoft 680d95c
Rename SortPomTest to SortPomMavenTest.
nedtwigg 4a80576
Create a no-maven SortPomTest which can run quickly.
nedtwigg f2c0e4a
Rename SortPomState to SortPomCfg, and:
nedtwigg bcd3735
FeatureClassLoader now loads `com.diffplug.spotless.glue.*` via `de…
nedtwigg 4b780c0
SortPomStep can now live within the normal `src/main/java`, and only …
nedtwigg 6d6ab66
Fix typo.
nedtwigg 24bc908
Fix FeatureClassLoader for eclipse entries.
nedtwigg 2b85d84
plugin-maven doesn't need any special dependency on the lib jar.
nedtwigg b95e93b
Generalize the `sortPom` stuff to facilitate #524 (glue code).
nedtwigg 617abf5
Added documentation for sortPom
tisoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* Copyright 2016-2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -15,11 +15,13 @@ | |
*/ | ||
package com.diffplug.spotless; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.nio.ByteBuffer; | ||
import java.security.ProtectionDomain; | ||
import java.util.Objects; | ||
|
||
import javax.annotation.Nullable; | ||
|
@@ -29,37 +31,31 @@ | |
* path of URLs.<br/> | ||
* Features shall be independent from build tools. Hence the class loader of the | ||
* underlying build tool is e.g. skipped during the the search for classes.<br/> | ||
* Only {@link #BUILD_TOOLS_PACKAGES } are explicitly looked up from the class loader of | ||
* the build tool and the provided URLs are ignored. This allows the feature to use | ||
* distinct functionality of the build tool. | ||
* | ||
* For `com.diffplug.spotless.glue.`, classes are redefined from within the lib jar | ||
* but linked against the `Url[]`. This allows us to ship classfiles which function as glue | ||
* code but delay linking/definition to runtime after the user has specified which version | ||
* of the formatter they want. | ||
* | ||
* For `"org.slf4j.` and (`com.diffplug.spotless.` but not `com.diffplug.spotless.extra.`) | ||
* the classes are loaded from the buildToolClassLoader. | ||
Comment on lines
+34
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fvgh, just FYI, we finally have a way to ship glue code without requiring all deps of that glue code. It works like so:
This will allow us to replace a lot of manual reflection code, ala #524. |
||
*/ | ||
class FeatureClassLoader extends URLClassLoader { | ||
static { | ||
ClassLoader.registerAsParallelCapable(); | ||
} | ||
|
||
/** | ||
* The following packages must be provided by the build tool or the corresponding Spotless plugin: | ||
* <ul> | ||
* <li>org.slf4j - SLF4J API must be provided. If no SLF4J binding is provided, log messages are dropped.</li> | ||
* </ul> | ||
*/ | ||
static final List<String> BUILD_TOOLS_PACKAGES = Collections.unmodifiableList(Arrays.asList("org.slf4j.")); | ||
// NOTE: if this changes, you need to also update the `JarState.getClassLoader` methods. | ||
|
||
private final ClassLoader buildToolClassLoader; | ||
|
||
/** | ||
* Constructs a new FeatureClassLoader for the given URLs, based on an {@code URLClassLoader}, | ||
* using the system class loader as parent. For {@link #BUILD_TOOLS_PACKAGES }, the build | ||
* tool class loader is used. | ||
* using the system class loader as parent. | ||
* | ||
* @param urls the URLs from which to load classes and resources | ||
* @param buildToolClassLoader The build tool class loader | ||
* @exception SecurityException If a security manager exists and prevents the creation of a class loader. | ||
* @exception NullPointerException if {@code urls} is {@code null}. | ||
*/ | ||
|
||
FeatureClassLoader(URL[] urls, ClassLoader buildToolClassLoader) { | ||
super(urls, getParentClassLoader()); | ||
Objects.requireNonNull(buildToolClassLoader); | ||
|
@@ -68,12 +64,53 @@ class FeatureClassLoader extends URLClassLoader { | |
|
||
@Override | ||
protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
for (String buildToolPackage : BUILD_TOOLS_PACKAGES) { | ||
if (name.startsWith(buildToolPackage)) { | ||
return buildToolClassLoader.loadClass(name); | ||
if (name.startsWith("com.diffplug.spotless.glue.")) { | ||
String path = name.replace('.', '/') + ".class"; | ||
URL url = findResource(path); | ||
if (url == null) { | ||
throw new ClassNotFoundException(name); | ||
} | ||
try { | ||
return defineClass(name, urlToByteBuffer(url), (ProtectionDomain) null); | ||
} catch (IOException e) { | ||
throw new ClassNotFoundException(name, e); | ||
} | ||
} else if (useBuildToolClassLoader(name)) { | ||
return buildToolClassLoader.loadClass(name); | ||
} else { | ||
return super.findClass(name); | ||
} | ||
} | ||
|
||
private static boolean useBuildToolClassLoader(String name) { | ||
if (name.startsWith("org.slf4j.")) { | ||
return true; | ||
} else if (!name.startsWith("com.diffplug.spotless.extra") && name.startsWith("com.diffplug.spotless.")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public URL findResource(String name) { | ||
URL resource = super.findResource(name); | ||
if (resource != null) { | ||
return resource; | ||
} | ||
return buildToolClassLoader.getResource(name); | ||
} | ||
|
||
private static ByteBuffer urlToByteBuffer(URL url) throws IOException { | ||
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
int nRead; | ||
byte[] data = new byte[1024]; | ||
InputStream inputStream = url.openStream(); | ||
while ((nRead = inputStream.read(data, 0, data.length)) != -1) { | ||
buffer.write(data, 0, nRead); | ||
} | ||
return super.findClass(name); | ||
buffer.flush(); | ||
return ByteBuffer.wrap(buffer.toByteArray()); | ||
} | ||
|
||
/** | ||
|
55 changes: 55 additions & 0 deletions
55
lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.pom; | ||
|
||
import java.io.Serializable; | ||
|
||
// Class and members must be public, otherwise we get failed to access class com.diffplug.spotless.pom.SortPomInternalState from class com.diffplug.spotless.pom.SortPomFormatterFunc (com.diffplug.spotless.pom.SortPomInternalState is in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @682bd3c4; com.diffplug.spotless.pom.SortPomFormatterFunc is in unnamed module of loader com.diffplug.spotless.pom.DelegatingClassLoader @573284a5) | ||
public class SortPomCfg implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public String encoding = "UTF-8"; | ||
|
||
public String lineSeparator = System.getProperty("line.separator"); | ||
|
||
public boolean expandEmptyElements = true; | ||
|
||
public boolean spaceBeforeCloseEmptyElement = false; | ||
|
||
public boolean keepBlankLines = true; | ||
|
||
public int nrOfIndentSpace = 2; | ||
|
||
public boolean indentBlankLines = false; | ||
|
||
public boolean indentSchemaLocation = false; | ||
|
||
public String predefinedSortOrder = "recommended_2008_06"; | ||
|
||
public String sortOrderFile = null; | ||
|
||
public String sortDependencies = null; | ||
|
||
public String sortDependencyExclusions = null; | ||
|
||
public String sortPlugins = null; | ||
|
||
public boolean sortProperties = false; | ||
|
||
public boolean sortModules = false; | ||
|
||
public boolean sortExecutions = false; | ||
} |
54 changes: 54 additions & 0 deletions
54
lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.pom; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
public class SortPomStep { | ||
public static final String NAME = "sortPom"; | ||
|
||
private SortPomStep() {} | ||
|
||
private SortPomCfg cfg; | ||
|
||
public static FormatterStep create(SortPomCfg cfg, Provisioner provisioner) { | ||
return FormatterStep.createLazy(NAME, () -> new State(cfg, provisioner), State::createFormat); | ||
} | ||
|
||
static class State implements Serializable { | ||
SortPomCfg cfg; | ||
JarState jarState; | ||
|
||
public State(SortPomCfg cfg, Provisioner provisioner) throws IOException { | ||
this.cfg = cfg; | ||
this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.0.0", provisioner); | ||
} | ||
|
||
FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | ||
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.pom.SortPomFormatterFunc"); | ||
Constructor<?> constructor = formatterFunc.getConstructor(SortPomCfg.class); | ||
return (FormatterFunc) constructor.newInstance(cfg); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
lib/src/sortPom/java/com/diffplug/spotless/glue/pom/SortPomFormatterFunc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.glue.pom; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.pom.SortPomCfg; | ||
|
||
import sortpom.SortPomImpl; | ||
import sortpom.logger.SortPomLogger; | ||
import sortpom.parameter.PluginParameters; | ||
|
||
public class SortPomFormatterFunc implements FormatterFunc { | ||
private static final Logger logger = Logger.getLogger(SortPomFormatterFunc.class.getName()); | ||
private final SortPomCfg cfg; | ||
|
||
public SortPomFormatterFunc(SortPomCfg cfg) { | ||
this.cfg = cfg; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
// SortPom expects a file to sort, so we write the inpout into a temporary file | ||
File pom = File.createTempFile("pom", ".xml"); | ||
pom.deleteOnExit(); | ||
IOUtils.write(input, new FileOutputStream(pom), cfg.encoding); | ||
SortPomImpl sortPom = new SortPomImpl(); | ||
sortPom.setup(new MySortPomLogger(), PluginParameters.builder() | ||
.setPomFile(pom) | ||
.setFileOutput(false, null, null, false) | ||
.setEncoding(cfg.encoding) | ||
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement, cfg.keepBlankLines) | ||
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation) | ||
.setSortOrder(cfg.sortOrderFile, cfg.predefinedSortOrder) | ||
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions) | ||
.setTriggers(false) | ||
.build()); | ||
sortPom.sortPom(); | ||
return IOUtils.toString(new FileInputStream(pom), cfg.encoding); | ||
} | ||
|
||
private static class MySortPomLogger implements SortPomLogger { | ||
@Override | ||
public void warn(String content) { | ||
logger.warning(content); | ||
} | ||
|
||
@Override | ||
public void info(String content) { | ||
logger.info(content); | ||
} | ||
|
||
@Override | ||
public void error(String content) { | ||
logger.severe(content); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.